package com.chengma.mapper;

import java.util.List;
import com.chengma.domain.TbComment;
import com.chengma.domain.vo.CommentTreeDTO;
import org.apache.ibatis.annotations.*;

/**
 * 评论Mapper接口
 * 
 * @author tck
 * @date 2025-03-19
 */
@Mapper
public interface TbCommentMapper 
{
    /**
     * 查询评论
     * 
     * @param id 评论主键
     * @return 评论
     */
    public TbComment selectTbCommentById(Long id);

    /**
     * 查询评论列表
     * 
     * @param tbComment 评论
     * @return 评论集合
     */
    public List<TbComment> selectTbCommentList(TbComment tbComment);

    /**
     * 新增评论
     * 
     * @param tbComment 评论
     * @return 结果
     */
    public int insertTbComment(TbComment tbComment);

    /**
     * 修改评论
     * 
     * @param tbComment 评论
     * @return 结果
     */
    public int updateTbComment(TbComment tbComment);

    /**
     * 删除评论
     * 
     * @param id 评论主键
     * @return 结果
     */
    public int deleteTbCommentById(Long id);

    /**
     * 批量删除评论
     * 
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteTbCommentByIds(Long[] ids);

    // 平铺查询
    @Select("SELECT id, comment as contentId, create_user, create_time, reply_id " +
            "FROM tb_comment WHERE question_id = #{questionId}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "replyId", column = "reply_id"),
            @Result(property = "content", column = "contentId",
                    one = @One(select = "getCommentContent")),
            @Result(property = "likes", column = "id",
                    one = @One(select = "getCommentLikes"))
    })
    List<CommentTreeDTO> getCommentsByQuestionId(String questionId);

    // 获取富文本内容
    @Select("SELECT html FROM tb_editor_data WHERE id = #{contentId}")
    String getCommentContent(Integer contentId);

    // 获取评论数
    @Select("SELECT COUNT(*) FROM tb_user_liked_comment WHERE comment_id = #{commentId}")
    Integer getCommentLikes(Integer commentId);

    // 用于汇总评论的评论数量
    @Select("SELECT COUNT(*) FROM tb_comment WHERE reply_id = #{commentId}")
    Integer getCommentLikesNUm(Integer commentId);
}
